<?php
/**
 * 授权.
 * User: dxk <1024114660@qq.com>
 * Date: 2022/4/7
 */
namespace App\Service\OpenApi\AdminApi;

use App\Service\Core\Admin\User\AdminUserService;
use App\Service\OpenApi\AppAuthTokenService;

class AppAuthService
{
    private $AppAuthTokenService;
    private string $app_key = '';

    public function __construct($app_key)
    {
        $this->app_key = trim($app_key);
        $this->AppAuthTokenService = new AppAuthTokenService($this->app_key);
    }

    /**
     * 用户取消授权
     * @param $token
     * @return array
     */
    public function cancelAuth($token): array
    {
        // 取消授权
        $this->AppAuthTokenService->deleteAuthToken($token);
        return alert_info(0, '已取消授权');
    }

    /**
     * 检测是授权
     * @param $token
     * @return array
     */
    public function checkAuth($token): array
    {
        $token_data = $this->AppAuthTokenService->getToken('ACCESSTOKEN:' . $token);
        if (empty($token_data) || !isset($token_data['app_key'])) {
            return alert_info(1, '登录超时，或者您已在其他地方登录，请重新登录');
        }
        // 判断此应用此用户是否授权
        if ($this->app_key != $token_data['app_key']) {
            return alert_info(1, '用户未授权');
        }
        if (!is_numeric($token_data['operate_id']) || $token_data['operate_id'] <= 0) {
            return alert_info(1, '用户信息异常，请重新授权');
        }
        if ($token_data['auth_expire_at'] < date('Y-m-d H:i:s')) {
            return alert_info(1, '用户access_token已到期');
        }

        return alert_info(0, 'OK', $token_data);
    }

    /**
     * 用户授权
     * @param $operate_id
     * @param array $params
     * @return array
     */
    public function auth($operate_id, array $params = []): array
    {
        // 检测是否可以授权
        $check_res = $this->checkIsAllowAuth($operate_id);
        if (!isset($check_res['code']) || $check_res['code'] !== 0) {
            return $check_res;
        }
        $operate_id = $check_res['data']['user_info']['user_id'];
        if ($operate_id <= 0) {
            return alert_info(1, '内部异常', $check_res);
        }
        // 授权
        $res = $this->AppAuthTokenService->createAuthToken($operate_id, 3 * 24 * 3600, $params);
        if ($res === false || !is_array($res)) {
            return alert_info(1, 'TOKEN生成异常');
        }
        return alert_info(0, 'OK', $res);
    }

    /**
     * 检测是否允许授权
     * @param int $operate_id 被授权用户ID
     * @return array
     */
    public function checkIsAllowAuth(int $operate_id): array
    {
        $operate_id = trim($operate_id);
        if (empty($this->app_key) || !is_numeric($operate_id)) {
            return alert_info(1, '非法请求002');
        }
        //检测应用是否可用
        $app_info = config('openapi.' . $this->app_key);
        if (empty($app_info)) {
            return alert_info(1, '不存在的应用');
        }
        //检测用户是否可用
        $check_res = AdminUserService::getInstance()->checkUserUsable($operate_id);
        if ($check_res['code'] !== 0) {
            return $check_res;
        }
        $data = ['user_info' => $check_res['data']['user_info'], 'app_info' => $app_info];
        return alert_info(0, 'OK', $data);
    }
}
